using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Samples.SsbTransportChannel;
using SsbTransportTestSuite.Proxies.OneWay;
using System.ServiceModel;
using SsbTransportTestSuite.Clients;
using System.Threading;
using System.Data.SqlClient;
using System.ServiceModel.Channels;

namespace SsbTransportTestSuite
{

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
    class TestTwoWayClient: IOrderNotification
    {

        ManualResetEvent orderShipped = new ManualResetEvent(false);
        
        
        internal void RunClient(Binding binding)
        {
            
            OrderProcessingClient client = new OrderProcessingClient(binding, new EndpointAddress(TestUtils.ServiceEndpointAddress));
                         
            Order order = new Order();
            order.OrderId = Guid.NewGuid();
            client.BeginOrder(order);
            OrderItem item1 = new OrderItem();
            OrderItem item2 = new OrderItem();
            //item2.Amount = -5;
            client.AddOrderItem(item1);
            client.AddOrderItem(item2);
            client.EndOrder();

            SsbConversationSender s = client.InnerChannel.GetProperty<SsbConversationSender>();
            ServiceHost host = s.CreateResponseHost<IOrderNotification>(this, new Uri(TestUtils.ClientEndpointAddress));
            host.Open();
   
            orderShipped.WaitOne();
            host.Close();
            client.Close();

        }

        void host_Faulted(object sender, EventArgs e)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        #region IOrderNotification Members

        void IOrderNotification.ConfirmOrder()
        {
            return;
        }

        void IOrderNotification.UpdateOrderStatus(OrderStatus status)
        {
            if (status == OrderStatus.Shipped)
            {
                orderShipped.Set();
            }
        }

        #endregion
    }
}
